package tw.twandroidproj.app.ui.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tw.twandroidproj.app.R;

public class mytime extends AppCompatActivity {

    private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();
    MaterialCalendarView calendar;
    Button choose_calendar;
    TextView certainTime;
    ListView timeList;
    private List<Map<String,Object>> hourList;
    Map<String,Object> hourmap= new HashMap<String,Object>();
    private SimpleAdapter time_simadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mytime);
        hourList = new ArrayList<Map<String,Object>>();
        choose_calendar = (Button) findViewById(R.id.choose_calender);
        certainTime = (TextView) findViewById(R.id.cerainTime);

        timeList = (ListView) findViewById(R.id.timeList);

        choose_calendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LayoutInflater inflater = LayoutInflater.from(mytime.this);
                final View dia_view = inflater.inflate(R.layout.calendar_dialog, null);
                AlertDialog.Builder builder = new AlertDialog.Builder(mytime.this);
                builder.setTitle("选择日期");
                builder.setView(dia_view);
                final AlertDialog dialog = builder.create();
                dialog.show();

                calendar = (MaterialCalendarView) dia_view.findViewById(R.id.calendarView); //设置日期改变监听事件
                final Button time_Certain = (Button) dia_view.findViewById(R.id.time_Certain);
                final Button time_cancel = (Button) dia_view.findViewById(R.id.time_cancel);

                time_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                calendar.setOnDateChangedListener(new OnDateSelectedListener() {
                    @Override
                    public void onDateSelected(MaterialCalendarView widget, final CalendarDay date, boolean selected) {
                        time_Certain.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                certainTime.setText(getSelectedDatesString());
                                dialog.dismiss();
                                time_simadapter = new SimpleAdapter(mytime.this, getHourData(), R.layout.mytime_item, new String[]{"hour_detail", "hour_job"}, new int[]{R.id.hour_detail, R.id.hour_job});
                                timeList.setAdapter(time_simadapter);
                            }
                        });
                    }
                });
            }
        });




    }
    private List<Map<String,Object>> getHourData() {
        hourList.clear();
        for (int i = 1; i <= 3; i++) {
            hourmap.put("hour_detail", "xx小时—xx小时");
            hourmap.put("hour_job", "工作");
            hourList.add(hourmap);

        }
        return hourList;
    }

    private String getSelectedDatesString() {
        CalendarDay date = calendar.getSelectedDate();
        if (date == null) {
            return "No Selection";
        }
        return FORMATTER.format(date.getDate());
    }
}
